/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.NonexistentEntityException;
import controladores.exceptions.PreexistingEntityException;
import entidades.Reservarestaurante;
import entidades.ReservarestaurantePK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.Reserva;

/**
 *
 * @author Andres
 */
public class ReservarestauranteJpaController {

    public ReservarestauranteJpaController() {
        emf = Persistence.createEntityManagerFactory("proyectoCongresoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Reservarestaurante reservarestaurante) throws PreexistingEntityException, Exception {
        if (reservarestaurante.getReservarestaurantePK() == null) {
            reservarestaurante.setReservarestaurantePK(new ReservarestaurantePK());
        }
        reservarestaurante.getReservarestaurantePK().setReservaCodigoreserva(reservarestaurante.getReserva().getCodigoreserva());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reserva reserva = reservarestaurante.getReserva();
            if (reserva != null) {
                reserva = em.getReference(reserva.getClass(), reserva.getCodigoreserva());
                reservarestaurante.setReserva(reserva);
            }
            em.persist(reservarestaurante);
            if (reserva != null) {
                reserva.getReservarestauranteList().add(reservarestaurante);
                reserva = em.merge(reserva);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findReservarestaurante(reservarestaurante.getReservarestaurantePK()) != null) {
                throw new PreexistingEntityException("Reservarestaurante " + reservarestaurante + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Reservarestaurante reservarestaurante) throws NonexistentEntityException, Exception {
        reservarestaurante.getReservarestaurantePK().setReservaCodigoreserva(reservarestaurante.getReserva().getCodigoreserva());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservarestaurante persistentReservarestaurante = em.find(Reservarestaurante.class, reservarestaurante.getReservarestaurantePK());
            Reserva reservaOld = persistentReservarestaurante.getReserva();
            Reserva reservaNew = reservarestaurante.getReserva();
            if (reservaNew != null) {
                reservaNew = em.getReference(reservaNew.getClass(), reservaNew.getCodigoreserva());
                reservarestaurante.setReserva(reservaNew);
            }
            reservarestaurante = em.merge(reservarestaurante);
            if (reservaOld != null && !reservaOld.equals(reservaNew)) {
                reservaOld.getReservarestauranteList().remove(reservarestaurante);
                reservaOld = em.merge(reservaOld);
            }
            if (reservaNew != null && !reservaNew.equals(reservaOld)) {
                reservaNew.getReservarestauranteList().add(reservarestaurante);
                reservaNew = em.merge(reservaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ReservarestaurantePK id = reservarestaurante.getReservarestaurantePK();
                if (findReservarestaurante(id) == null) {
                    throw new NonexistentEntityException("The reservarestaurante with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ReservarestaurantePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservarestaurante reservarestaurante;
            try {
                reservarestaurante = em.getReference(Reservarestaurante.class, id);
                reservarestaurante.getReservarestaurantePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The reservarestaurante with id " + id + " no longer exists.", enfe);
            }
            Reserva reserva = reservarestaurante.getReserva();
            if (reserva != null) {
                reserva.getReservarestauranteList().remove(reservarestaurante);
                reserva = em.merge(reserva);
            }
            em.remove(reservarestaurante);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Reservarestaurante> findReservarestauranteEntities() {
        return findReservarestauranteEntities(true, -1, -1);
    }

    public List<Reservarestaurante> findReservarestauranteEntities(int maxResults, int firstResult) {
        return findReservarestauranteEntities(false, maxResults, firstResult);
    }

    private List<Reservarestaurante> findReservarestauranteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Reservarestaurante.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Reservarestaurante findReservarestaurante(ReservarestaurantePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Reservarestaurante.class, id);
        } finally {
            em.close();
        }
    }

    public int getReservarestauranteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Reservarestaurante> rt = cq.from(Reservarestaurante.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
